"""
用户视图层
"""

from sys import flags
from interface import user_interface,bank_interface,shop_interface
from lib import common
#全局变量记录用户登录状态
loginer = None
# 1、注册功能
def regiter():
    while True:
        #（1）让用户输入用户名与密码进行校验
        user_name = input("请输入id：")
        password = input("请输入密码：") 
        repassword = input("请再输入一次密码：")
        # balance = input("输入初始金额：")
        # 小逻辑:判断俩次密码是否相同
        if password == repassword:
            #(2)调用接口层的注册接口，将用户名和密码交给接口层处理
            flag,msg = user_interface.register_interface(user_name,password)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print("俩次密码不同，请重新输入!")
                         

# 2、登录功能
def login():
    while True:
        user_name = input("请输入用户账号：")
        password = input("请输入用户密码：")
        flag,img = user_interface.login_interface(user_name,password)
        if flag:
            print(img)
            #记录用户登录状态
            global loginer
            loginer = user_name
            break
        else:
            print(img)
            
# 3、查看余额
@common.out
def check_balance():
    money = bank_interface.select_banlace(loginer)
    print(f"用户{loginer}的余额为{money}")

# 4、提现功能
@common.out
def withdraw():
    while True:
        money = input("请输入体现金额：")
        if not money.isdigit():
            print("请输入正确的金额")
            continue
        money = int(money)
        flag,msg = bank_interface.getmoney(loginer,money)
        if flag:
            print(msg)
            break
        else:
            print(msg)

# 5、还款功能
@common.out
def repay():
    while True:
        money = input("输入还款金额:")
        if not money.isdigit():
            print("请输入正确的金额")
            continue
        money = int(money)
        if money > 0:
            flag,msg = bank_interface.backmoney(loginer,money)
        if flag:
            print(msg)
            break
# 6、转账功能
@common.out
def transfer():
    while True:
        people = input("请输入转账对象：")
        money = input("请输入转账金额：")
        if not money.isdigit():
            print("请输入正确的金额格式")
            continue 
        money = int(money)
        if money > 0:
            flag,msg = bank_interface.transfer(loginer,people,money)
            if flag:
                print(msg)
                break
            else:
                print(msg)
        else:
            print("请输入正确的金额格式")

# 7、查看流水
@common.out
def check_flow():
    flag,msg = bank_interface.check_flow(loginer)
    if flag:
        for i in msg:
            print(i)
    else:
        print(msg)
        
# 8、购物功能
@common.out
def shopping():
    shop_car = {}#{"小滑头":[5,10个]}
    shop_list = [
            ["小滑头",5],
            ["乐事薯片",6],
            ["可乐",3],
            ["鸡腿",8],
            ["泡面",6],
            ["蚊香",20],
            ["公仔",50],
        ]
    while True:
        show_list =enumerate(shop_list)
        print("============欢迎来到商城=========")
        for index,shop in show_list:
            shop_name,price = shop
            print(f"编号:{index}-名称:{shop_name}-单价:{price}")
        print("================end==============")

        #支付或保存购物车功能
        choice = input("输入商品编号添加到购物车(输入y购买当前购物车,输入n保存当前购物车)")
        if choice == "y":
            if not shop_car:
                print("购物车为空！")
                continue
            flag,msg = shop_interface.pay(loginer,shop_car)
            if flag:
                print(msg)
            else:
                print(msg)
        if choice == "n":
            if not shop_car:
                print("购物车为空！")
                continue
            flag,msg = shop_interface.save(loginer,shop_car)
            if flag:
                print(msg)
        
               
        #添加到购物车
        if not choice.isdigit():
            print("请输入正确的编号")
            continue 
        choice = int(choice)
        if choice not in range(len(shop_list)):
            print("请输入正确的编号")
            continue
        name1,price1 = shop_list[choice]
 
        if name1 in shop_car:
            shop_car[name1][1] += 1
            print(f"购物车{shop_car}")
        else:
            shop_car[name1] = [price1,1]
            print(f"购物车{shop_car}")



# 9、查看购物车
@common.out
def check_shop_car():
    flag,msg = shop_interface.select_car(loginer)
    if flag:
        print(msg)
    else:
        print(msg)

# 10、管理员功能
def admin():
    from core import admin
    admin.admin_run()


#功能字典
func_dic ={
        "1":regiter,
        "2":login,
        "3":check_balance,
        "4":withdraw,
        "5":repay,
        "6":transfer,
        "7":check_flow,
        "8":shopping,
        "9":check_shop_car,
        "10":admin,
        }

#视图层主程序
def run():
    while True:
        print("""
        =====ATM——购物车======
        1、注册功能
        2、登录功能
        3、查看余额
        4、提现功能
        5、还款功能
        6、转账功能
        7、查看流水
        8、购物功能
        9、查看购物车
        10、管理员功能
        ========end==========
        """)
        gn = input("选择一个功能编号")
        if gn not in func_dic:
            print("选择正确的编号")
            continue
        func_dic.get(gn)()
            